<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>perlfaq8 - System Interaction - Perldoc Browser</title>
    <link rel="search" href="https://perldoc.perl.org/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser">
    <link rel="canonical" href="https://perldoc.perl.org/perlfaq8">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" />
    <style>
      body {
        background: #f4f4f5;
        color: #020202;
      }
      .navbar-dark {
        background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%);
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0);
        background-repeat: repeat-x;
      }
      .navbar-dark .navbar-nav .nav-link,
      .navbar-dark .navbar-nav .nav-link:focus { color: #fff }
      .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 }
      #wrapperlicious {
        margin: 0 auto;
        font: 0.9em 'Helvetica Neue', Helvetica, sans-serif;
        font-weight: normal;
        line-height: 1.5em;
        margin: 0;
        padding: 0;
      }
      #wrapperlicious h1 { font-size: 1.5em }
      #wrapperlicious h2 { font-size: 1.3em }
      #wrapperlicious h3 { font-size: 1.1em }
      #wrapperlicious h4 { font-size: 0.9em }
      #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3,
      #wrapperlicious h4, #wrapperlicious dt {
        color: #020202;
        margin-top: 1em;
        margin-bottom: 1em;
        position: relative;
        font-weight: bold;
      }
      #wrapperlicious a { color: inherit; text-decoration: underline }
      #wrapperlicious #toc { text-decoration: none }
      #wrapperlicious a:hover { color: #2a2a2a }
      #wrapperlicious a img { border: 0 }
      #wrapperlicious :not(pre) > code {
        color: inherit;
        background-color: rgba(0, 0, 0, 0.04);
        border-radius: 3px;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        padding: 0.3em;
      }
      #wrapperlicious dd {
        margin: 0;
        margin-left: 2em;
      }
      #wrapperlicious dt {
        color: #2a2a2a;
        font-weight: bold;
        margin-left: 0.9em;
      }
      #wrapperlicious p {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious li > p {
        margin-bottom: 0;
        margin-top: 0;
      }
      #wrapperlicious pre {
        border: 1px solid #c1c1c1;
        border-radius: 3px;
        font: 100% Consolas, Menlo, Monaco, monospace;
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious pre > code {
        display: block;
        background-color: #f6f6f6;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        line-height: 1.5em;
        text-align: left;
        white-space: pre;
        padding: 1em;
      }
      #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious ul {
        list-style-type: square;
      }
      #wrapperlicious ul ul {
        margin-bottom: 0px;
        margin-top: 0px;
      }
      #footer {
        font-size: 0.8em;
        padding-top: 0.5em;
        text-align: center;
      }
      #more {
        display: inline;
        font-size: 0.8em;
      }
      #perldocdiv {
        background-color: #fff;
        border: 1px solid #c1c1c1;
        border-bottom-left-radius: 5px;
        border-bottom-right-radius: 5px;
        margin-left: auto;
        margin-right: auto;
        padding: 3em;
        padding-top: 1em;
        max-width: 960px;
      }
      #moduleversion { float: right }
      #wrapperlicious .leading-notice {
        font-style: italic;
        padding-left: 1em;
        margin-top: 1em;
        margin-bottom: 1em;
      }
      #wrapperlicious .permalink {
        display: none;
        left: -0.75em;
        position: absolute;
        padding-right: 0.25em;
        text-decoration: none;
      }
      #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink,
      #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink,
      #wrapperlicious dt:hover .permalink {
        display: block;
      }
    </style>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'G-KVNWBNT5FB');
      gtag('config', 'UA-50555-3');
    </script>
  </head>
  <body>
    <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="https://perldoc.perl.org/">Perldoc Browser</a>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="perlfaq8.html#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.35.0</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-stable">
          <a class="dropdown-item" href="https://perldoc.perl.org/perlfaq8">Latest</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.2/perlfaq8">5.38.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.1/perlfaq8">5.38.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0/perlfaq8">5.38.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.3/perlfaq8">5.36.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.2/perlfaq8">5.36.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.1/perlfaq8">5.36.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.0/perlfaq8">5.36.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.3/perlfaq8">5.34.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.2/perlfaq8">5.34.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.1/perlfaq8">5.34.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.0/perlfaq8">5.34.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.1/perlfaq8">5.32.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.0/perlfaq8">5.32.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.3/perlfaq8">5.30.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.2/perlfaq8">5.30.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.1/perlfaq8">5.30.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.0/perlfaq8">5.30.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.3/perlfaq8">5.28.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.2/perlfaq8">5.28.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.1/perlfaq8">5.28.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.0/perlfaq8">5.28.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.3/perlfaq8">5.26.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.2/perlfaq8">5.26.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.1/perlfaq8">5.26.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.0/perlfaq8">5.26.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.4/perlfaq8">5.24.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.3/perlfaq8">5.24.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.2/perlfaq8">5.24.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.1/perlfaq8">5.24.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.0/perlfaq8">5.24.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.4/perlfaq8">5.22.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.3/perlfaq8">5.22.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.2/perlfaq8">5.22.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.1/perlfaq8">5.22.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.0/perlfaq8">5.22.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.3/perlfaq8">5.20.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.2/perlfaq8">5.20.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.1/perlfaq8">5.20.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.0/perlfaq8">5.20.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.4/perlfaq8">5.18.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.3/perlfaq8">5.18.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.2/perlfaq8">5.18.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.1/perlfaq8">5.18.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.0/perlfaq8">5.18.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.3/perlfaq8">5.16.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.2/perlfaq8">5.16.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.1/perlfaq8">5.16.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.0/perlfaq8">5.16.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.4/perlfaq8">5.14.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.3/perlfaq8">5.14.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.2/perlfaq8">5.14.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.1/perlfaq8">5.14.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.0/perlfaq8">5.14.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.5/perlfaq8">5.12.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.4/perlfaq8">5.12.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.3/perlfaq8">5.12.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.2/perlfaq8">5.12.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.1/perlfaq8">5.12.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.0/perlfaq8">5.12.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.1/perlfaq8">5.10.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.0/perlfaq8">5.10.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.9/perlfaq8">5.8.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.8/perlfaq8">5.8.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.7/perlfaq8">5.8.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.6/perlfaq8">5.8.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.5/perlfaq8">5.8.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.4/perlfaq8">5.8.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.3/perlfaq8">5.8.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.2/perlfaq8">5.8.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.1/perlfaq8">5.8.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.0/perlfaq8">5.8.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.2/perlfaq8">5.6.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.1/perlfaq8">5.6.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.0/perlfaq8">5.6.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_04/perlfaq8">5.005_04</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_03/perlfaq8">5.005_03</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_02/perlfaq8">5.005_02</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_01/perlfaq8">5.005_01</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005/perlfaq8">5.005</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="perlfaq8.html#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-dev">
          <a class="dropdown-item" href="https://perldoc.perl.org/blead/perlfaq8">blead</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.6/perlfaq8">5.39.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.5/perlfaq8">5.39.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.4/perlfaq8">5.39.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.3/perlfaq8">5.39.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.2/perlfaq8">5.39.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.1/perlfaq8">5.39.1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC2/perlfaq8">5.38.0-RC2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC1/perlfaq8">5.38.0-RC1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.11/perlfaq8">5.37.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.10/perlfaq8">5.37.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.9/perlfaq8">5.37.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.8/perlfaq8">5.37.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.7/perlfaq8">5.37.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.6/perlfaq8">5.37.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.5/perlfaq8">5.37.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.4/perlfaq8">5.37.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.3/perlfaq8">5.37.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.2/perlfaq8">5.37.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.1/perlfaq8">5.37.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.0/perlfaq8">5.37.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.11/perlfaq8">5.35.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.10/perlfaq8">5.35.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.9/perlfaq8">5.35.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.8/perlfaq8">5.35.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.7/perlfaq8">5.35.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.6/perlfaq8">5.35.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.5/perlfaq8">5.35.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.4/perlfaq8">5.35.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.3/perlfaq8">5.35.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.2/perlfaq8">5.35.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.1/perlfaq8">5.35.1</a>
          <a class="dropdown-item active" href="perlfaq8.html">5.35.0</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="perlfaq8.html#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-nav">
          <a class="dropdown-item" href="perl.html">Perl</a>
          <a class="dropdown-item" href="perlintro.html">Intro</a>
          <a class="dropdown-item" href="perl.html#Tutorials">Tutorials</a>
          <a class="dropdown-item" href="perlfaq.html">FAQs</a>
          <a class="dropdown-item" href="perl.html#Reference-Manual">Reference</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlop.html">Operators</a>
          <a class="dropdown-item" href="functions.html">Functions</a>
          <a class="dropdown-item" href="variables.html">Variables</a>
          <a class="dropdown-item" href="modules.html">Modules</a>
          <a class="dropdown-item" href="perlutil.html">Utilities</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlcommunity.html">Community</a>
          <a class="dropdown-item" href="perlhist.html">History</a>
        </div>
      </li>
    </ul>
    <ul class="navbar-nav">
      <script>
        function set_expand (expand) {
          var perldocdiv = document.getElementById('perldocdiv');
          var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width');
          var expanded = (width == '' || width == 'none') ? true : false;
          if (expand === null) {
            expand = !expanded;
          }
          if ((expand && !expanded) || (!expand && expanded)) {
            perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px');
            var button_classlist = document.getElementById('content-expand-button').classList;
            if (expand) {
              button_classlist.add('btn-light');
              button_classlist.remove('btn-outline-light');
            } else {
              button_classlist.add('btn-outline-light');
              button_classlist.remove('btn-light');
            }
          }
          return expand;
        }
        function toggle_expand () {
          var expand = set_expand(null);
          document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC';
        }
        function read_expand () {
          return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 });
        }
        if (document.readyState === 'loading') {
          document.addEventListener('DOMContentLoaded', function () {
            if (read_expand()) {
              set_expand(true);
            }
          });
        } else if (read_expand()) {
          set_expand(true);
        }
      </script>
      <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-2" onclick="toggle_expand()">Expand</button>
    <ul>
    <form class="form-inline" method="get" action="https://perldoc.perl.org/5.35.0/search">
      <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search">
    </form>
  </div>
</nav>

    <div id="wrapperlicious" class="container-fluid">
      <div id="perldocdiv">
        <div id="links">
          <a href="perlfaq8.html">perlfaq8</a>
          <div id="more">
            (<a href="perlfaq8.txt">source</a>,
            <a href="https://metacpan.org/pod/perlfaq8">CPAN</a>)
          </div>
        </div>
        <div class="leading-notice">
          You are viewing the version of this documentation from Perl 5.35.0.
            This is a development version of Perl.
        </div>
        <h1><a id="toc">CONTENTS</a></h1>
                  <ul>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#NAME">NAME</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#VERSION">VERSION</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#DESCRIPTION">DESCRIPTION</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-find-out-which-operating-system-I&#39;m-running-under?">How do I find out which operating system I&#39;m running under?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-come-exec()-doesn&#39;t-return?">How come exec() doesn&#39;t return?     </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-do-fancy-stuff-with-the-keyboard/screen/mouse?">How do I do fancy stuff with the keyboard/screen/mouse?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-print-something-out-in-color?">How do I print something out in color?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-read-just-one-key-without-waiting-for-a-return-key?">How do I read just one key without waiting for a return key?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-check-whether-input-is-ready-on-the-keyboard?">How do I check whether input is ready on the keyboard?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-clear-the-screen?">How do I clear the screen?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-get-the-screen-size?">How do I get the screen size?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-ask-the-user-for-a-password?">How do I ask the user for a password?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-read-and-write-the-serial-port?">How do I read and write the serial port?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-decode-encrypted-password-files?">How do I decode encrypted password files?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-start-a-process-in-the-background?">How do I start a process in the background?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-trap-control-characters/signals?">How do I trap control characters/signals?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-modify-the-shadow-password-file-on-a-Unix-system?">How do I modify the shadow password file on a Unix system?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-set-the-time-and-date?">How do I set the time and date?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-can-I-sleep()-or-alarm()-for-under-a-second?">How can I sleep() or alarm() for under a second?    </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-can-I-measure-time-under-a-second?">How can I measure time under a second?    </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-can-I-do-an-atexit()-or-setjmp()/longjmp()?-(Exception-handling)">How can I do an atexit() or setjmp()/longjmp()? (Exception handling)</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#Why-doesn&#39;t-my-sockets-program-work-under-System-V-(Solaris)?-What-does-the-error-message-%22Protocol-not-supported%22-mean?">Why doesn&#39;t my sockets program work under System V (Solaris)? What does the error message &quot;Protocol not supported&quot; mean?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-can-I-call-my-system&#39;s-unique-C-functions-from-Perl?">How can I call my system&#39;s unique C functions from Perl?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#Where-do-I-get-the-include-files-to-do-ioctl()-or-syscall()?">Where do I get the include files to do ioctl() or syscall()?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#Why-do-setuid-perl-scripts-complain-about-kernel-problems?">Why do setuid perl scripts complain about kernel problems?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-can-I-open-a-pipe-both-to-and-from-a-command?">How can I open a pipe both to and from a command?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#Why-can&#39;t-I-get-the-output-of-a-command-with-system()?">Why can&#39;t I get the output of a command with system()?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-can-I-capture-STDERR-from-an-external-command?">How can I capture STDERR from an external command?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#Why-doesn&#39;t-open()-return-an-error-when-a-pipe-open-fails?">Why doesn&#39;t open() return an error when a pipe open fails?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#What&#39;s-wrong-with-using-backticks-in-a-void-context?">What&#39;s wrong with using backticks in a void context?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-can-I-call-backticks-without-shell-processing?">How can I call backticks without shell processing?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#Why-can&#39;t-my-script-read-from-STDIN-after-I-gave-it-EOF-(%5ED-on-Unix,-%5EZ-on-MS-DOS)?">Why can&#39;t my script read from STDIN after I gave it EOF (^D on Unix, ^Z on MS-DOS)?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-can-I-convert-my-shell-script-to-perl?">How can I convert my shell script to perl?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#Can-I-use-perl-to-run-a-telnet-or-ftp-session?">Can I use perl to run a telnet or ftp session?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-can-I-write-expect-in-Perl?">How can I write expect in Perl?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#Is-there-a-way-to-hide-perl&#39;s-command-line-from-programs-such-as-%22ps%22?">Is there a way to hide perl&#39;s command line from programs such as &quot;ps&quot;?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#I-%7Bchanged-directory,-modified-my-environment%7D-in-a-perl-script.-How-come-the-change-disappeared-when-I-exited-the-script?-How-do-I-get-my-changes-to-be-visible?">I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-close-a-process&#39;s-filehandle-without-waiting-for-it-to-complete?">How do I close a process&#39;s filehandle without waiting for it to complete?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-fork-a-daemon-process?">How do I fork a daemon process?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-find-out-if-I&#39;m-running-interactively-or-not?">How do I find out if I&#39;m running interactively or not?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-timeout-a-slow-event?">How do I timeout a slow event?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-set-CPU-limits?">How do I set CPU limits?   </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-avoid-zombies-on-a-Unix-system?">How do I avoid zombies on a Unix system?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-use-an-SQL-database?">How do I use an SQL database?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-make-a-system()-exit-on-control-C?">How do I make a system() exit on control-C?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-open-a-file-without-blocking?">How do I open a file without blocking?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-tell-the-difference-between-errors-from-the-shell-and-perl?">How do I tell the difference between errors from the shell and perl?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-install-a-module-from-CPAN?">How do I install a module from CPAN?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#What&#39;s-the-difference-between-require-and-use?">What&#39;s the difference between require and use?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-keep-my-own-module/library-directory?">How do I keep my own module/library directory?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-add-the-directory-my-program-lives-in-to-the-module/library-search-path?">How do I add the directory my program lives in to the module/library search path?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#How-do-I-add-a-directory-to-my-include-path-(@INC)-at-runtime?">How do I add a directory to my include path (@INC) at runtime?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#Where-are-modules-installed?">Where are modules installed?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#What-is-socket.ph-and-where-do-I-get-it?">What is socket.ph and where do I get it?</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq8.html#AUTHOR-AND-COPYRIGHT">AUTHOR AND COPYRIGHT</a>
              </li>
          </ul>

      <h1 id="NAME"><a class="permalink" href="perlfaq8.html#NAME">#</a>NAME</h1>

<p>perlfaq8 - System Interaction</p>

<h1 id="VERSION"><a class="permalink" href="perlfaq8.html#VERSION">#</a>VERSION</h1>

<p>version 5.20210411</p>

<h1 id="DESCRIPTION"><a class="permalink" href="perlfaq8.html#DESCRIPTION">#</a>DESCRIPTION</h1>

<p>This section of the Perl FAQ covers questions involving operating system interaction. Topics include interprocess communication (IPC), control over the user-interface (keyboard, screen and pointing devices), and most anything else not related to data manipulation.</p>

<p>Read the FAQs and documentation specific to the port of perl to your operating system (eg, <a href="perlvms.html">perlvms</a>, <a href="perlplan9.html">perlplan9</a>, ...). These should contain more detailed information on the vagaries of your perl.</p>

<h2 id="How-do-I-find-out-which-operating-system-I&#39;m-running-under?"><a class="permalink" href="perlfaq8.html#How-do-I-find-out-which-operating-system-I&#39;m-running-under?">#</a><a id="How"></a><a id="How-do-I-find-out-which-operating-system-Im-running-under"></a>How do I find out which operating system I&#39;m running under?</h2>

<p>The <code>$^O</code> variable (<code>$OSNAME</code> if you use <code>English</code>) contains an indication of the name of the operating system (not its release number) that your perl binary was built for.</p>

<h2 id="How-come-exec()-doesn&#39;t-return?"><a class="permalink" href="perlfaq8.html#How-come-exec()-doesn&#39;t-return?">#</a><a id="How1"></a><a id="How-come-exec-doesnt-return"></a>How come exec() doesn&#39;t return?     </h2>

<p>(contributed by brian d foy)</p>

<p>The <code>exec</code> function&#39;s job is to turn your process into another command and never to return. If that&#39;s not what you want to do, don&#39;t use <code>exec</code>. :)</p>

<p>If you want to run an external command and still keep your Perl process going, look at a piped <code>open</code>, <code>fork</code>, or <code>system</code>.</p>

<h2 id="How-do-I-do-fancy-stuff-with-the-keyboard/screen/mouse?"><a class="permalink" href="perlfaq8.html#How-do-I-do-fancy-stuff-with-the-keyboard/screen/mouse?">#</a><a id="How2"></a><a id="How-do-I-do-fancy-stuff-with-the-keyboard-screen-mouse"></a>How do I do fancy stuff with the keyboard/screen/mouse?</h2>

<p>How you access/control keyboards, screens, and pointing devices (&quot;mice&quot;) is system-dependent. Try the following modules:</p>

<dl>

<dt id="Keyboard"><a class="permalink" href="perlfaq8.html#Keyboard">#</a>Keyboard</dt>
<dd>

<pre><code class="plaintext">Term::Cap               Standard perl distribution
Term::ReadKey           CPAN
Term::ReadLine::Gnu     CPAN
Term::ReadLine::Perl    CPAN
Term::Screen            CPAN</code></pre>

</dd>
<dt id="Screen"><a class="permalink" href="perlfaq8.html#Screen">#</a>Screen</dt>
<dd>

<pre><code class="plaintext">Term::Cap               Standard perl distribution
Curses                  CPAN
Term::ANSIColor         CPAN</code></pre>

</dd>
<dt id="Mouse"><a class="permalink" href="perlfaq8.html#Mouse">#</a>Mouse</dt>
<dd>

<pre><code class="plaintext">Tk                      CPAN
Wx                      CPAN
Gtk2                    CPAN
Qt4                     kdebindings4 package</code></pre>

</dd>
</dl>

<p>Some of these specific cases are shown as examples in other answers in this section of the perlfaq.</p>

<h2 id="How-do-I-print-something-out-in-color?"><a class="permalink" href="perlfaq8.html#How-do-I-print-something-out-in-color?">#</a><a id="How3"></a><a id="How-do-I-print-something-out-in-color"></a>How do I print something out in color?</h2>

<p>In general, you don&#39;t, because you don&#39;t know whether the recipient has a color-aware display device. If you know that they have an ANSI terminal that understands color, you can use the <a href="./Term::ANSIColor.html">Term::ANSIColor</a> module from CPAN:</p>

<pre><code>use Term::ANSIColor;
print color(&quot;red&quot;), &quot;Stop!\n&quot;, color(&quot;reset&quot;);
print color(&quot;green&quot;), &quot;Go!\n&quot;, color(&quot;reset&quot;);</code></pre>

<p>Or like this:</p>

<pre><code>use Term::ANSIColor qw(:constants);
print RED, &quot;Stop!\n&quot;, RESET;
print GREEN, &quot;Go!\n&quot;, RESET;</code></pre>

<h2 id="How-do-I-read-just-one-key-without-waiting-for-a-return-key?"><a class="permalink" href="perlfaq8.html#How-do-I-read-just-one-key-without-waiting-for-a-return-key?">#</a><a id="How4"></a><a id="How-do-I-read-just-one-key-without-waiting-for-a-return-key"></a>How do I read just one key without waiting for a return key?</h2>

<p>Controlling input buffering is a remarkably system-dependent matter. On many systems, you can just use the <b>stty</b> command as shown in <a href="perlfunc.html#getc">&quot;getc&quot; in perlfunc</a>, but as you see, that&#39;s already getting you into portability snags.</p>

<pre><code>open(TTY, &quot;+&lt;/dev/tty&quot;) or die &quot;no tty: $!&quot;;
system &quot;stty  cbreak &lt;/dev/tty &gt;/dev/tty 2&gt;&amp;1&quot;;
$key = getc(TTY);        # perhaps this works
# OR ELSE
sysread(TTY, $key, 1);    # probably this does
system &quot;stty -cbreak &lt;/dev/tty &gt;/dev/tty 2&gt;&amp;1&quot;;</code></pre>

<p>The <a href="./Term::ReadKey.html">Term::ReadKey</a> module from CPAN offers an easy-to-use interface that should be more efficient than shelling out to <b>stty</b> for each key. It even includes limited support for Windows.</p>

<pre><code>use Term::ReadKey;
ReadMode(&#39;cbreak&#39;);
$key = ReadKey(0);
ReadMode(&#39;normal&#39;);</code></pre>

<p>However, using the code requires that you have a working C compiler and can use it to build and install a CPAN module. Here&#39;s a solution using the standard <a href="POSIX.html">POSIX</a> module, which is already on your system (assuming your system supports POSIX).</p>

<pre><code>use HotKey;
$key = readkey();</code></pre>

<p>And here&#39;s the <code>HotKey</code> module, which hides the somewhat mystifying calls to manipulate the POSIX termios structures.</p>

<pre><code># HotKey.pm
package HotKey;

use strict;
use warnings;

use parent &#39;Exporter&#39;;
our @EXPORT = qw(cbreak cooked readkey);

use POSIX qw(:termios_h);
my ($term, $oterm, $echo, $noecho, $fd_stdin);

$fd_stdin = fileno(STDIN);
$term     = POSIX::Termios-&gt;new();
$term-&gt;getattr($fd_stdin);
$oterm     = $term-&gt;getlflag();

$echo     = ECHO | ECHOK | ICANON;
$noecho   = $oterm &amp; ~$echo;

sub cbreak {
    $term-&gt;setlflag($noecho);  # ok, so i don&#39;t want echo either
    $term-&gt;setcc(VTIME, 1);
    $term-&gt;setattr($fd_stdin, TCSANOW);
}

sub cooked {
    $term-&gt;setlflag($oterm);
    $term-&gt;setcc(VTIME, 0);
    $term-&gt;setattr($fd_stdin, TCSANOW);
}

sub readkey {
    my $key = &#39;&#39;;
    cbreak();
    sysread(STDIN, $key, 1);
    cooked();
    return $key;
}

END { cooked() }

1;</code></pre>

<h2 id="How-do-I-check-whether-input-is-ready-on-the-keyboard?"><a class="permalink" href="perlfaq8.html#How-do-I-check-whether-input-is-ready-on-the-keyboard?">#</a><a id="How5"></a><a id="How-do-I-check-whether-input-is-ready-on-the-keyboard"></a>How do I check whether input is ready on the keyboard?</h2>

<p>The easiest way to do this is to read a key in nonblocking mode with the <a href="./Term::ReadKey.html">Term::ReadKey</a> module from CPAN, passing it an argument of -1 to indicate not to block:</p>

<pre><code>use Term::ReadKey;

ReadMode(&#39;cbreak&#39;);

if (defined (my $char = ReadKey(-1)) ) {
    # input was waiting and it was $char
} else {
    # no input was waiting
}

ReadMode(&#39;normal&#39;);                  # restore normal tty settings</code></pre>

<h2 id="How-do-I-clear-the-screen?"><a class="permalink" href="perlfaq8.html#How-do-I-clear-the-screen?">#</a><a id="How6"></a><a id="How-do-I-clear-the-screen"></a>How do I clear the screen?</h2>

<p>(contributed by brian d foy)</p>

<p>To clear the screen, you just have to print the special sequence that tells the terminal to clear the screen. Once you have that sequence, output it when you want to clear the screen.</p>

<p>You can use the <a href="./Term::ANSIScreen.html">Term::ANSIScreen</a> module to get the special sequence. Import the <code>cls</code> function (or the <code>:screen</code> tag):</p>

<pre><code>use Term::ANSIScreen qw(cls);
my $clear_screen = cls();

print $clear_screen;</code></pre>

<p>The <a href="./Term::Cap.html">Term::Cap</a> module can also get the special sequence if you want to deal with the low-level details of terminal control. The <code>Tputs</code> method returns the string for the given capability:</p>

<pre><code>use Term::Cap;

my $terminal = Term::Cap-&gt;Tgetent( { OSPEED =&gt; 9600 } );
my $clear_screen = $terminal-&gt;Tputs(&#39;cl&#39;);

print $clear_screen;</code></pre>

<p>On Windows, you can use the <a href="./Win32::Console.html">Win32::Console</a> module. After creating an object for the output filehandle you want to affect, call the <code>Cls</code> method:</p>

<pre><code>Win32::Console;

my $OUT = Win32::Console-&gt;new(STD_OUTPUT_HANDLE);
my $clear_string = $OUT-&gt;Cls;

print $clear_screen;</code></pre>

<p>If you have a command-line program that does the job, you can call it in backticks to capture whatever it outputs so you can use it later:</p>

<pre><code>my $clear_string = `clear`;

print $clear_string;</code></pre>

<h2 id="How-do-I-get-the-screen-size?"><a class="permalink" href="perlfaq8.html#How-do-I-get-the-screen-size?">#</a><a id="How7"></a><a id="How-do-I-get-the-screen-size"></a>How do I get the screen size?</h2>

<p>If you have <a href="./Term::ReadKey.html">Term::ReadKey</a> module installed from CPAN, you can use it to fetch the width and height in characters and in pixels:</p>

<pre><code>use Term::ReadKey;
my ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize();</code></pre>

<p>This is more portable than the raw <code>ioctl</code>, but not as illustrative:</p>

<pre><code>require &#39;./sys/ioctl.ph&#39;;
die &quot;no TIOCGWINSZ &quot; unless defined &amp;TIOCGWINSZ;
open(my $tty_fh, &quot;+&lt;/dev/tty&quot;)                     or die &quot;No tty: $!&quot;;
unless (ioctl($tty_fh, &amp;TIOCGWINSZ, $winsize=&#39;&#39;)) {
    die sprintf &quot;$0: ioctl TIOCGWINSZ (%08x: $!)\n&quot;, &amp;TIOCGWINSZ;
}
my ($row, $col, $xpixel, $ypixel) = unpack(&#39;S4&#39;, $winsize);
print &quot;(row,col) = ($row,$col)&quot;;
print &quot;  (xpixel,ypixel) = ($xpixel,$ypixel)&quot; if $xpixel || $ypixel;
print &quot;\n&quot;;</code></pre>

<h2 id="How-do-I-ask-the-user-for-a-password?"><a class="permalink" href="perlfaq8.html#How-do-I-ask-the-user-for-a-password?">#</a><a id="How8"></a><a id="How-do-I-ask-the-user-for-a-password"></a>How do I ask the user for a password?</h2>

<p>(This question has nothing to do with the web. See a different FAQ for that.)</p>

<p>There&#39;s an example of this in <a href="perlfunc.html#crypt">&quot;crypt&quot; in perlfunc</a>. First, you put the terminal into &quot;no echo&quot; mode, then just read the password normally. You may do this with an old-style <code>ioctl()</code> function, POSIX terminal control (see <a href="POSIX.html">POSIX</a> or its documentation the Camel Book), or a call to the <b>stty</b> program, with varying degrees of portability.</p>

<p>You can also do this for most systems using the <a href="./Term::ReadKey.html">Term::ReadKey</a> module from CPAN, which is easier to use and in theory more portable.</p>

<pre><code>use Term::ReadKey;

ReadMode(&#39;noecho&#39;);
my $password = ReadLine(0);</code></pre>

<h2 id="How-do-I-read-and-write-the-serial-port?"><a class="permalink" href="perlfaq8.html#How-do-I-read-and-write-the-serial-port?">#</a><a id="How9"></a><a id="How-do-I-read-and-write-the-serial-port"></a>How do I read and write the serial port?</h2>

<p>This depends on which operating system your program is running on. In the case of Unix, the serial ports will be accessible through files in <code>/dev</code>; on other systems, device names will doubtless differ. Several problem areas common to all device interaction are the following:</p>

<dl>

<dt id="lockfiles"><a class="permalink" href="perlfaq8.html#lockfiles">#</a>lockfiles</dt>
<dd>

<p>Your system may use lockfiles to control multiple access. Make sure you follow the correct protocol. Unpredictable behavior can result from multiple processes reading from one device.</p>

</dd>
<dt id="open-mode"><a class="permalink" href="perlfaq8.html#open-mode">#</a><a id="open"></a>open mode</dt>
<dd>

<p>If you expect to use both read and write operations on the device, you&#39;ll have to open it for update (see <a href="perlfunc.html#open">&quot;open&quot; in perlfunc</a> for details). You may wish to open it without running the risk of blocking by using <code>sysopen()</code> and <code>O_RDWR|O_NDELAY|O_NOCTTY</code> from the <a href="Fcntl.html">Fcntl</a> module (part of the standard perl distribution). See <a href="perlfunc.html#sysopen">&quot;sysopen&quot; in perlfunc</a> for more on this approach.</p>

</dd>
<dt id="end-of-line"><a class="permalink" href="perlfaq8.html#end-of-line">#</a><a id="end"></a>end of line</dt>
<dd>

<p>Some devices will be expecting a &quot;\r&quot; at the end of each line rather than a &quot;\n&quot;. In some ports of perl, &quot;\r&quot; and &quot;\n&quot; are different from their usual (Unix) ASCII values of &quot;\015&quot; and &quot;\012&quot;. You may have to give the numeric values you want directly, using octal (&quot;\015&quot;), hex (&quot;0x0D&quot;), or as a control-character specification (&quot;\cM&quot;).</p>

<pre><code>print DEV &quot;atv1\012&quot;;    # wrong, for some devices
print DEV &quot;atv1\015&quot;;    # right, for some devices</code></pre>

<p>Even though with normal text files a &quot;\n&quot; will do the trick, there is still no unified scheme for terminating a line that is portable between Unix, DOS/Win, and Macintosh, except to terminate <i>ALL</i> line ends with &quot;\015\012&quot;, and strip what you don&#39;t need from the output. This applies especially to socket I/O and autoflushing, discussed next.</p>

</dd>
<dt id="flushing-output"><a class="permalink" href="perlfaq8.html#flushing-output">#</a><a id="flushing"></a>flushing output</dt>
<dd>

<p>If you expect characters to get to your device when you <code>print()</code> them, you&#39;ll want to autoflush that filehandle. You can use <code>select()</code> and the <code>$|</code> variable to control autoflushing (see <a href="perlvar.html#%24%7C">&quot;$|&quot; in perlvar</a> and <a href="perlfunc.html#select">&quot;select&quot; in perlfunc</a>, or <a href="perlfaq5.html">perlfaq5</a>, &quot;How do I flush/unbuffer an output filehandle? Why must I do this?&quot;):</p>

<pre><code>my $old_handle = select($dev_fh);
$| = 1;
select($old_handle);</code></pre>

<p>You&#39;ll also see code that does this without a temporary variable, as in</p>

<pre><code>select((select($deb_handle), $| = 1)[0]);</code></pre>

<p>Or if you don&#39;t mind pulling in a few thousand lines of code just because you&#39;re afraid of a little <code>$|</code> variable:</p>

<pre><code>use IO::Handle;
$dev_fh-&gt;autoflush(1);</code></pre>

<p>As mentioned in the previous item, this still doesn&#39;t work when using socket I/O between Unix and Macintosh. You&#39;ll need to hard code your line terminators, in that case.</p>

</dd>
<dt id="non-blocking-input"><a class="permalink" href="perlfaq8.html#non-blocking-input">#</a><a id="non"></a>non-blocking input</dt>
<dd>

<p>If you are doing a blocking <code>read()</code> or <code>sysread()</code>, you&#39;ll have to arrange for an alarm handler to provide a timeout (see <a href="perlfunc.html#alarm">&quot;alarm&quot; in perlfunc</a>). If you have a non-blocking open, you&#39;ll likely have a non-blocking read, which means you may have to use a 4-arg <code>select()</code> to determine whether I/O is ready on that device (see <a href="perlfunc.html#select">&quot;select&quot; in perlfunc</a>.</p>

</dd>
</dl>

<p>While trying to read from his caller-id box, the notorious Jamie Zawinski <code>&lt;jwz@netscape.com&gt;</code>, after much gnashing of teeth and fighting with <code>sysread</code>, <code>sysopen</code>, POSIX&#39;s <code>tcgetattr</code> business, and various other functions that go bump in the night, finally came up with this:</p>

<pre><code>sub open_modem {
    use IPC::Open2;
    my $stty = `/bin/stty -g`;
    open2( \*MODEM_IN, \*MODEM_OUT, &quot;cu -l$modem_device -s2400 2&gt;&amp;1&quot;);
    # starting cu hoses /dev/tty&#39;s stty settings, even when it has
    # been opened on a pipe...
    system(&quot;/bin/stty $stty&quot;);
    $_ = &lt;MODEM_IN&gt;;
    chomp;
    if ( !m/^Connected/ ) {
        print STDERR &quot;$0: cu printed `$_&#39; instead of `Connected&#39;\n&quot;;
    }
}</code></pre>

<h2 id="How-do-I-decode-encrypted-password-files?"><a class="permalink" href="perlfaq8.html#How-do-I-decode-encrypted-password-files?">#</a><a id="How10"></a><a id="How-do-I-decode-encrypted-password-files"></a>How do I decode encrypted password files?</h2>

<p>You spend lots and lots of money on dedicated hardware, but this is bound to get you talked about.</p>

<p>Seriously, you can&#39;t if they are Unix password files--the Unix password system employs one-way encryption. It&#39;s more like hashing than encryption. The best you can do is check whether something else hashes to the same string. You can&#39;t turn a hash back into the original string. Programs like Crack can forcibly (and intelligently) try to guess passwords, but don&#39;t (can&#39;t) guarantee quick success.</p>

<p>If you&#39;re worried about users selecting bad passwords, you should proactively check when they try to change their password (by modifying <a href="http://man.he.net/man1/passwd">passwd(1)</a>, for example).</p>

<h2 id="How-do-I-start-a-process-in-the-background?"><a class="permalink" href="perlfaq8.html#How-do-I-start-a-process-in-the-background?">#</a><a id="How11"></a><a id="How-do-I-start-a-process-in-the-background"></a>How do I start a process in the background?</h2>

<p>(contributed by brian d foy)</p>

<p>There&#39;s not a single way to run code in the background so you don&#39;t have to wait for it to finish before your program moves on to other tasks. Process management depends on your particular operating system, and many of the techniques are covered in <a href="perlipc.html">perlipc</a>.</p>

<p>Several CPAN modules may be able to help, including <a href="./IPC::Open2.html">IPC::Open2</a> or <a href="./IPC::Open3.html">IPC::Open3</a>, <a href="./IPC::Run.html">IPC::Run</a>, <a href="./Parallel::Jobs.html">Parallel::Jobs</a>, <a href="./Parallel::ForkManager.html">Parallel::ForkManager</a>, <a href="POE.html">POE</a>, <a href="./Proc::Background.html">Proc::Background</a>, and <a href="./Win32::Process.html">Win32::Process</a>. There are many other modules you might use, so check those namespaces for other options too.</p>

<p>If you are on a Unix-like system, you might be able to get away with a system call where you put an <code>&amp;</code> on the end of the command:</p>

<pre><code class="plaintext">system(&quot;cmd &amp;&quot;)</code></pre>

<p>You can also try using <code>fork</code>, as described in <a href="perlfunc.html">perlfunc</a> (although this is the same thing that many of the modules will do for you).</p>

<dl>

<dt id="STDIN,-STDOUT,-and-STDERR-are-shared"><a class="permalink" href="perlfaq8.html#STDIN,-STDOUT,-and-STDERR-are-shared">#</a><a id="STDIN"></a><a id="STDIN-STDOUT-and-STDERR-are-shared"></a>STDIN, STDOUT, and STDERR are shared</dt>
<dd>

<p>Both the main process and the backgrounded one (the &quot;child&quot; process) share the same STDIN, STDOUT and STDERR filehandles. If both try to access them at once, strange things can happen. You may want to close or reopen these for the child. You can get around this with <code>open</code>ing a pipe (see <a href="perlfunc.html#open">&quot;open&quot; in perlfunc</a>) but on some systems this means that the child process cannot outlive the parent.</p>

</dd>
<dt id="Signals"><a class="permalink" href="perlfaq8.html#Signals">#</a>Signals</dt>
<dd>

<p>You&#39;ll have to catch the SIGCHLD signal, and possibly SIGPIPE too. SIGCHLD is sent when the backgrounded process finishes. SIGPIPE is sent when you write to a filehandle whose child process has closed (an untrapped SIGPIPE can cause your program to silently die). This is not an issue with <code>system(&quot;cmd&amp;&quot;)</code>.</p>

</dd>
<dt id="Zombies"><a class="permalink" href="perlfaq8.html#Zombies">#</a>Zombies</dt>
<dd>

<p>You have to be prepared to &quot;reap&quot; the child process when it finishes.</p>

<pre><code>$SIG{CHLD} = sub { wait };

$SIG{CHLD} = &#39;IGNORE&#39;;</code></pre>

<p>You can also use a double fork. You immediately <code>wait()</code> for your first child, and the init daemon will <code>wait()</code> for your grandchild once it exits.</p>

<pre><code>unless ($pid = fork) {
    unless (fork) {
        exec &quot;what you really wanna do&quot;;
        die &quot;exec failed!&quot;;
    }
    exit 0;
}
waitpid($pid, 0);</code></pre>

<p>See <a href="perlipc.html#Signals">&quot;Signals&quot; in perlipc</a> for other examples of code to do this. Zombies are not an issue with <code>system(&quot;prog &amp;&quot;)</code>.</p>

</dd>
</dl>

<h2 id="How-do-I-trap-control-characters/signals?"><a class="permalink" href="perlfaq8.html#How-do-I-trap-control-characters/signals?">#</a><a id="How12"></a><a id="How-do-I-trap-control-characters-signals"></a>How do I trap control characters/signals?</h2>

<p>You don&#39;t actually &quot;trap&quot; a control character. Instead, that character generates a signal which is sent to your terminal&#39;s currently foregrounded process group, which you then trap in your process. Signals are documented in <a href="perlipc.html#Signals">&quot;Signals&quot; in perlipc</a> and the section on &quot;Signals&quot; in the Camel.</p>

<p>You can set the values of the <code>%SIG</code> hash to be the functions you want to handle the signal. After perl catches the signal, it looks in <code>%SIG</code> for a key with the same name as the signal, then calls the subroutine value for that key.</p>

<pre><code># as an anonymous subroutine

$SIG{INT} = sub { syswrite(STDERR, &quot;ouch\n&quot;, 5 ) };

# or a reference to a function

$SIG{INT} = \&amp;ouch;

# or the name of the function as a string

$SIG{INT} = &quot;ouch&quot;;</code></pre>

<p>Perl versions before 5.8 had in its C source code signal handlers which would catch the signal and possibly run a Perl function that you had set in <code>%SIG</code>. This violated the rules of signal handling at that level causing perl to dump core. Since version 5.8.0, perl looks at <code>%SIG</code> <b>after</b> the signal has been caught, rather than while it is being caught. Previous versions of this answer were incorrect.</p>

<h2 id="How-do-I-modify-the-shadow-password-file-on-a-Unix-system?"><a class="permalink" href="perlfaq8.html#How-do-I-modify-the-shadow-password-file-on-a-Unix-system?">#</a><a id="How13"></a><a id="How-do-I-modify-the-shadow-password-file-on-a-Unix-system"></a>How do I modify the shadow password file on a Unix system?</h2>

<p>If perl was installed correctly and your shadow library was written properly, the <code>getpw*()</code> functions described in <a href="perlfunc.html">perlfunc</a> should in theory provide (read-only) access to entries in the shadow password file. To change the file, make a new shadow password file (the format varies from system to system--see <a href="http://man.he.net/man1/passwd">passwd(1)</a> for specifics) and use <code>pwd_mkdb(8)</code> to install it (see <a href="http://man.he.net/man8/pwd_mkdb">pwd_mkdb(8)</a> for more details).</p>

<h2 id="How-do-I-set-the-time-and-date?"><a class="permalink" href="perlfaq8.html#How-do-I-set-the-time-and-date?">#</a><a id="How14"></a><a id="How-do-I-set-the-time-and-date"></a>How do I set the time and date?</h2>

<p>Assuming you&#39;re running under sufficient permissions, you should be able to set the system-wide date and time by running the <code>date(1)</code> program. (There is no way to set the time and date on a per-process basis.) This mechanism will work for Unix, MS-DOS, Windows, and NT; the VMS equivalent is <code>set time</code>.</p>

<p>However, if all you want to do is change your time zone, you can probably get away with setting an environment variable:</p>

<pre><code>$ENV{TZ} = &quot;MST7MDT&quot;;           # Unixish
$ENV{&#39;SYS$TIMEZONE_DIFFERENTIAL&#39;}=&quot;-5&quot; # vms
system(&#39;trn&#39;, &#39;comp.lang.perl.misc&#39;);</code></pre>

<h2 id="How-can-I-sleep()-or-alarm()-for-under-a-second?"><a class="permalink" href="perlfaq8.html#How-can-I-sleep()-or-alarm()-for-under-a-second?">#</a><a id="How15"></a><a id="How-can-I-sleep-or-alarm-for-under-a-second"></a>How can I sleep() or alarm() for under a second?    </h2>

<p>If you want finer granularity than the 1 second that the <code>sleep()</code> function provides, the easiest way is to use the <code>select()</code> function as documented in <a href="perlfunc.html#select">&quot;select&quot; in perlfunc</a>. Try the <a href="./Time::HiRes.html">Time::HiRes</a> and the <a href="./BSD::Itimer.html">BSD::Itimer</a> modules (available from CPAN, and starting from Perl 5.8 <a href="./Time::HiRes.html">Time::HiRes</a> is part of the standard distribution).</p>

<h2 id="How-can-I-measure-time-under-a-second?"><a class="permalink" href="perlfaq8.html#How-can-I-measure-time-under-a-second?">#</a><a id="How16"></a><a id="How-can-I-measure-time-under-a-second"></a>How can I measure time under a second?    </h2>

<p>(contributed by brian d foy)</p>

<p>The <a href="./Time::HiRes.html">Time::HiRes</a> module (part of the standard distribution as of Perl 5.8) measures time with the <code>gettimeofday()</code> system call, which returns the time in microseconds since the epoch. If you can&#39;t install <a href="./Time::HiRes.html">Time::HiRes</a> for older Perls and you are on a Unixish system, you may be able to call <code>gettimeofday(2)</code> directly. See <a href="perlfunc.html#syscall">&quot;syscall&quot; in perlfunc</a>.</p>

<h2 id="How-can-I-do-an-atexit()-or-setjmp()/longjmp()?-(Exception-handling)"><a class="permalink" href="perlfaq8.html#How-can-I-do-an-atexit()-or-setjmp()/longjmp()?-(Exception-handling)">#</a><a id="How17"></a><a id="How-can-I-do-an-atexit-or-setjmp-longjmp-Exception-handling"></a>How can I do an atexit() or setjmp()/longjmp()? (Exception handling)</h2>

<p>You can use the <code>END</code> block to simulate <code>atexit()</code>. Each package&#39;s <code>END</code> block is called when the program or thread ends. See the <a href="perlmod.html">perlmod</a> manpage for more details about <code>END</code> blocks.</p>

<p>For example, you can use this to make sure your filter program managed to finish its output without filling up the disk:</p>

<pre><code>END {
    close(STDOUT) || die &quot;stdout close failed: $!&quot;;
}</code></pre>

<p>The <code>END</code> block isn&#39;t called when untrapped signals kill the program, though, so if you use <code>END</code> blocks you should also use</p>

<pre><code>use sigtrap qw(die normal-signals);</code></pre>

<p>Perl&#39;s exception-handling mechanism is its <code>eval()</code> operator. You can use <code>eval()</code> as <code>setjmp</code> and <code>die()</code> as <code>longjmp</code>. For details of this, see the section on signals, especially the time-out handler for a blocking <code>flock()</code> in <a href="perlipc.html#Signals">&quot;Signals&quot; in perlipc</a> or the section on &quot;Signals&quot; in <i>Programming Perl</i>.</p>

<p>If exception handling is all you&#39;re interested in, use one of the many CPAN modules that handle exceptions, such as <a href="./Try::Tiny.html">Try::Tiny</a>.</p>

<p>If you want the <code>atexit()</code> syntax (and an <code>rmexit()</code> as well), try the <code>AtExit</code> module available from CPAN.</p>

<h2 id="Why-doesn&#39;t-my-sockets-program-work-under-System-V-(Solaris)?-What-does-the-error-message-&quot;Protocol-not-supported&quot;-mean?"><a class="permalink" href="perlfaq8.html#Why-doesn&#39;t-my-sockets-program-work-under-System-V-(Solaris)?-What-does-the-error-message-%22Protocol-not-supported%22-mean?">#</a><a id="Why"></a><a id="Why-doesnt-my-sockets-program-work-under-System-V-Solaris-What-does-the-error-message-Protocol-not-supported-mean"></a>Why doesn&#39;t my sockets program work under System V (Solaris)? What does the error message &quot;Protocol not supported&quot; mean?</h2>

<p>Some Sys-V based systems, notably Solaris 2.X, redefined some of the standard socket constants. Since these were constant across all architectures, they were often hardwired into perl code. The proper way to deal with this is to &quot;use Socket&quot; to get the correct values.</p>

<p>Note that even though SunOS and Solaris are binary compatible, these values are different. Go figure.</p>

<h2 id="How-can-I-call-my-system&#39;s-unique-C-functions-from-Perl?"><a class="permalink" href="perlfaq8.html#How-can-I-call-my-system&#39;s-unique-C-functions-from-Perl?">#</a><a id="How18"></a><a id="How-can-I-call-my-systems-unique-C-functions-from-Perl"></a>How can I call my system&#39;s unique C functions from Perl?</h2>

<p>In most cases, you write an external module to do it--see the answer to &quot;Where can I learn about linking C with Perl? [h2xs, xsubpp]&quot;. However, if the function is a system call, and your system supports <code>syscall()</code>, you can use the <code>syscall</code> function (documented in <a href="perlfunc.html">perlfunc</a>).</p>

<p>Remember to check the modules that came with your distribution, and CPAN as well--someone may already have written a module to do it. On Windows, try <a href="./Win32::API.html">Win32::API</a>. On Macs, try <a href="./Mac::Carbon.html">Mac::Carbon</a>. If no module has an interface to the C function, you can inline a bit of C in your Perl source with <a href="./Inline::C.html">Inline::C</a>.</p>

<h2 id="Where-do-I-get-the-include-files-to-do-ioctl()-or-syscall()?"><a class="permalink" href="perlfaq8.html#Where-do-I-get-the-include-files-to-do-ioctl()-or-syscall()?">#</a><a id="Where"></a><a id="Where-do-I-get-the-include-files-to-do-ioctl-or-syscall"></a>Where do I get the include files to do ioctl() or syscall()?</h2>

<p>Historically, these would be generated by the <a href="h2ph.html">h2ph</a> tool, part of the standard perl distribution. This program converts <code>cpp(1)</code> directives in C header files to files containing subroutine definitions, like <code>SYS_getitimer()</code>, which you can use as arguments to your functions. It doesn&#39;t work perfectly, but it usually gets most of the job done. Simple files like <i>errno.h</i>, <i>syscall.h</i>, and <i>socket.h</i> were fine, but the hard ones like <i>ioctl.h</i> nearly always need to be hand-edited. Here&#39;s how to install the *.ph files:</p>

<pre><code class="plaintext">1. Become the super-user
2. cd /usr/include
3. h2ph *.h */*.h</code></pre>

<p>If your system supports dynamic loading, for reasons of portability and sanity you probably ought to use <a href="h2xs.html">h2xs</a> (also part of the standard perl distribution). This tool converts C header files to Perl extensions. See <a href="perlxstut.html">perlxstut</a> for how to get started with <a href="h2xs.html">h2xs</a>.</p>

<p>If your system doesn&#39;t support dynamic loading, you still probably ought to use <a href="h2xs.html">h2xs</a>. See <a href="perlxstut.html">perlxstut</a> and <a href="./ExtUtils::MakeMaker.html">ExtUtils::MakeMaker</a> for more information (in brief, just use <b>make perl</b> instead of a plain <b>make</b> to rebuild perl with a new static extension).</p>

<h2 id="Why-do-setuid-perl-scripts-complain-about-kernel-problems?"><a class="permalink" href="perlfaq8.html#Why-do-setuid-perl-scripts-complain-about-kernel-problems?">#</a><a id="Why1"></a><a id="Why-do-setuid-perl-scripts-complain-about-kernel-problems"></a>Why do setuid perl scripts complain about kernel problems?</h2>

<p>Some operating systems have bugs in the kernel that make setuid scripts inherently insecure. Perl gives you a number of options (described in <a href="perlsec.html">perlsec</a>) to work around such systems.</p>

<h2 id="How-can-I-open-a-pipe-both-to-and-from-a-command?"><a class="permalink" href="perlfaq8.html#How-can-I-open-a-pipe-both-to-and-from-a-command?">#</a><a id="How19"></a><a id="How-can-I-open-a-pipe-both-to-and-from-a-command"></a>How can I open a pipe both to and from a command?</h2>

<p>The <a href="./IPC::Open2.html">IPC::Open2</a> module (part of the standard perl distribution) is an easy-to-use approach that internally uses <code>pipe()</code>, <code>fork()</code>, and <code>exec()</code> to do the job. Make sure you read the deadlock warnings in its documentation, though (see <a href="./IPC::Open2.html">IPC::Open2</a>). See <a href="perlipc.html#Bidirectional-Communication-with-Another-Process">&quot;Bidirectional Communication with Another Process&quot; in perlipc</a> and <a href="perlipc.html#Bidirectional-Communication-with-Yourself">&quot;Bidirectional Communication with Yourself&quot; in perlipc</a></p>

<p>You may also use the <a href="./IPC::Open3.html">IPC::Open3</a> module (part of the standard perl distribution), but be warned that it has a different order of arguments from <a href="./IPC::Open2.html">IPC::Open2</a> (see <a href="./IPC::Open3.html">IPC::Open3</a>).</p>

<h2 id="Why-can&#39;t-I-get-the-output-of-a-command-with-system()?"><a class="permalink" href="perlfaq8.html#Why-can&#39;t-I-get-the-output-of-a-command-with-system()?">#</a><a id="Why2"></a><a id="Why-cant-I-get-the-output-of-a-command-with-system"></a>Why can&#39;t I get the output of a command with system()?</h2>

<p>You&#39;re confusing the purpose of <code>system()</code> and backticks (``). <code>system()</code> runs a command and returns exit status information (as a 16 bit value: the low 7 bits are the signal the process died from, if any, and the high 8 bits are the actual exit value). Backticks (``) run a command and return what it sent to STDOUT.</p>

<pre><code>my $exit_status   = system(&quot;mail-users&quot;);
my $output_string = `ls`;</code></pre>

<h2 id="How-can-I-capture-STDERR-from-an-external-command?"><a class="permalink" href="perlfaq8.html#How-can-I-capture-STDERR-from-an-external-command?">#</a><a id="How20"></a><a id="How-can-I-capture-STDERR-from-an-external-command"></a>How can I capture STDERR from an external command?</h2>

<p>There are three basic ways of running external commands:</p>

<pre><code>system $cmd;        # using system()
my $output = `$cmd`;        # using backticks (``)
open (my $pipe_fh, &quot;$cmd |&quot;);    # using open()</code></pre>

<p>With <code>system()</code>, both STDOUT and STDERR will go the same place as the script&#39;s STDOUT and STDERR, unless the <code>system()</code> command redirects them. Backticks and <code>open()</code> read <b>only</b> the STDOUT of your command.</p>

<p>You can also use the <code>open3()</code> function from <a href="./IPC::Open3.html">IPC::Open3</a>. Benjamin Goldberg provides some sample code:</p>

<p>To capture a program&#39;s STDOUT, but discard its STDERR:</p>

<pre><code>use IPC::Open3;
use File::Spec;
my $in = &#39;&#39;;
open(NULL, &quot;&gt;&quot;, File::Spec-&gt;devnull);
my $pid = open3($in, \*PH, &quot;&gt;&amp;NULL&quot;, &quot;cmd&quot;);
while( &lt;PH&gt; ) { }
waitpid($pid, 0);</code></pre>

<p>To capture a program&#39;s STDERR, but discard its STDOUT:</p>

<pre><code>use IPC::Open3;
use File::Spec;
my $in = &#39;&#39;;
open(NULL, &quot;&gt;&quot;, File::Spec-&gt;devnull);
my $pid = open3($in, &quot;&gt;&amp;NULL&quot;, \*PH, &quot;cmd&quot;);
while( &lt;PH&gt; ) { }
waitpid($pid, 0);</code></pre>

<p>To capture a program&#39;s STDERR, and let its STDOUT go to our own STDERR:</p>

<pre><code>use IPC::Open3;
my $in = &#39;&#39;;
my $pid = open3($in, &quot;&gt;&amp;STDERR&quot;, \*PH, &quot;cmd&quot;);
while( &lt;PH&gt; ) { }
waitpid($pid, 0);</code></pre>

<p>To read both a command&#39;s STDOUT and its STDERR separately, you can redirect them to temp files, let the command run, then read the temp files:</p>

<pre><code>use IPC::Open3;
use IO::File;
my $in = &#39;&#39;;
local *CATCHOUT = IO::File-&gt;new_tmpfile;
local *CATCHERR = IO::File-&gt;new_tmpfile;
my $pid = open3($in, &quot;&gt;&amp;CATCHOUT&quot;, &quot;&gt;&amp;CATCHERR&quot;, &quot;cmd&quot;);
waitpid($pid, 0);
seek $_, 0, 0 for \*CATCHOUT, \*CATCHERR;
while( &lt;CATCHOUT&gt; ) {}
while( &lt;CATCHERR&gt; ) {}</code></pre>

<p>But there&#39;s no real need for <b>both</b> to be tempfiles... the following should work just as well, without deadlocking:</p>

<pre><code>use IPC::Open3;
my $in = &#39;&#39;;
use IO::File;
local *CATCHERR = IO::File-&gt;new_tmpfile;
my $pid = open3($in, \*CATCHOUT, &quot;&gt;&amp;CATCHERR&quot;, &quot;cmd&quot;);
while( &lt;CATCHOUT&gt; ) {}
waitpid($pid, 0);
seek CATCHERR, 0, 0;
while( &lt;CATCHERR&gt; ) {}</code></pre>

<p>And it&#39;ll be faster, too, since we can begin processing the program&#39;s stdout immediately, rather than waiting for the program to finish.</p>

<p>With any of these, you can change file descriptors before the call:</p>

<pre><code>open(STDOUT, &quot;&gt;logfile&quot;);
system(&quot;ls&quot;);</code></pre>

<p>or you can use Bourne shell file-descriptor redirection:</p>

<pre><code>$output = `$cmd 2&gt;some_file`;
open (PIPE, &quot;cmd 2&gt;some_file |&quot;);</code></pre>

<p>You can also use file-descriptor redirection to make STDERR a duplicate of STDOUT:</p>

<pre><code>$output = `$cmd 2&gt;&amp;1`;
open (PIPE, &quot;cmd 2&gt;&amp;1 |&quot;);</code></pre>

<p>Note that you <i>cannot</i> simply open STDERR to be a dup of STDOUT in your Perl program and avoid calling the shell to do the redirection. This doesn&#39;t work:</p>

<pre><code>open(STDERR, &quot;&gt;&amp;STDOUT&quot;);
$alloutput = `cmd args`;  # stderr still escapes</code></pre>

<p>This fails because the <code>open()</code> makes STDERR go to where STDOUT was going at the time of the <code>open()</code>. The backticks then make STDOUT go to a string, but don&#39;t change STDERR (which still goes to the old STDOUT).</p>

<p>Note that you <i>must</i> use Bourne shell (<code>sh(1)</code>) redirection syntax in backticks, not <code>csh(1)</code>! Details on why Perl&#39;s <code>system()</code> and backtick and pipe opens all use the Bourne shell are in the <i>versus/csh.whynot</i> article in the &quot;Far More Than You Ever Wanted To Know&quot; collection in <a href="http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz">http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz</a> . To capture a command&#39;s STDERR and STDOUT together:</p>

<pre><code>$output = `cmd 2&gt;&amp;1`;                       # either with backticks
$pid = open(PH, &quot;cmd 2&gt;&amp;1 |&quot;);              # or with an open pipe
while (&lt;PH&gt;) { }                            #    plus a read</code></pre>

<p>To capture a command&#39;s STDOUT but discard its STDERR:</p>

<pre><code>$output = `cmd 2&gt;/dev/null`;                # either with backticks
$pid = open(PH, &quot;cmd 2&gt;/dev/null |&quot;);       # or with an open pipe
while (&lt;PH&gt;) { }                            #    plus a read</code></pre>

<p>To capture a command&#39;s STDERR but discard its STDOUT:</p>

<pre><code>$output = `cmd 2&gt;&amp;1 1&gt;/dev/null`;           # either with backticks
$pid = open(PH, &quot;cmd 2&gt;&amp;1 1&gt;/dev/null |&quot;);  # or with an open pipe
while (&lt;PH&gt;) { }                            #    plus a read</code></pre>

<p>To exchange a command&#39;s STDOUT and STDERR in order to capture the STDERR but leave its STDOUT to come out our old STDERR:</p>

<pre><code>$output = `cmd 3&gt;&amp;1 1&gt;&amp;2 2&gt;&amp;3 3&gt;&amp;-`;        # either with backticks
$pid = open(PH, &quot;cmd 3&gt;&amp;1 1&gt;&amp;2 2&gt;&amp;3 3&gt;&amp;-|&quot;);# or with an open pipe
while (&lt;PH&gt;) { }                            #    plus a read</code></pre>

<p>To read both a command&#39;s STDOUT and its STDERR separately, it&#39;s easiest to redirect them separately to files, and then read from those files when the program is done:</p>

<pre><code>system(&quot;program args 1&gt;program.stdout 2&gt;program.stderr&quot;);</code></pre>

<p>Ordering is important in all these examples. That&#39;s because the shell processes file descriptor redirections in strictly left to right order.</p>

<pre><code>system(&quot;prog args 1&gt;tmpfile 2&gt;&amp;1&quot;);
system(&quot;prog args 2&gt;&amp;1 1&gt;tmpfile&quot;);</code></pre>

<p>The first command sends both standard out and standard error to the temporary file. The second command sends only the old standard output there, and the old standard error shows up on the old standard out.</p>

<h2 id="Why-doesn&#39;t-open()-return-an-error-when-a-pipe-open-fails?"><a class="permalink" href="perlfaq8.html#Why-doesn&#39;t-open()-return-an-error-when-a-pipe-open-fails?">#</a><a id="Why3"></a><a id="Why-doesnt-open-return-an-error-when-a-pipe-open-fails"></a>Why doesn&#39;t open() return an error when a pipe open fails?</h2>

<p>If the second argument to a piped <code>open()</code> contains shell metacharacters, perl <code>fork()</code>s, then <code>exec()</code>s a shell to decode the metacharacters and eventually run the desired program. If the program couldn&#39;t be run, it&#39;s the shell that gets the message, not Perl. All your Perl program can find out is whether the shell itself could be successfully started. You can still capture the shell&#39;s STDERR and check it for error messages. See <a href="perlfaq8.html#How-can-I-capture-STDERR-from-an-external-command%3F">&quot;How can I capture STDERR from an external command?&quot;</a> elsewhere in this document, or use the <a href="./IPC::Open3.html">IPC::Open3</a> module.</p>

<p>If there are no shell metacharacters in the argument of <code>open()</code>, Perl runs the command directly, without using the shell, and can correctly report whether the command started.</p>

<h2 id="What&#39;s-wrong-with-using-backticks-in-a-void-context?"><a class="permalink" href="perlfaq8.html#What&#39;s-wrong-with-using-backticks-in-a-void-context?">#</a><a id="What"></a><a id="Whats-wrong-with-using-backticks-in-a-void-context"></a>What&#39;s wrong with using backticks in a void context?</h2>

<p>Strictly speaking, nothing. Stylistically speaking, it&#39;s not a good way to write maintainable code. Perl has several operators for running external commands. Backticks are one; they collect the output from the command for use in your program. The <code>system</code> function is another; it doesn&#39;t do this.</p>

<p>Writing backticks in your program sends a clear message to the readers of your code that you wanted to collect the output of the command. Why send a clear message that isn&#39;t true?</p>

<p>Consider this line:</p>

<pre><code>`cat /etc/termcap`;</code></pre>

<p>You forgot to check <code>$?</code> to see whether the program even ran correctly. Even if you wrote</p>

<pre><code>print `cat /etc/termcap`;</code></pre>

<p>this code could and probably should be written as</p>

<pre><code>system(&quot;cat /etc/termcap&quot;) == 0
or die &quot;cat program failed!&quot;;</code></pre>

<p>which will echo the cat command&#39;s output as it is generated, instead of waiting until the program has completed to print it out. It also checks the return value.</p>

<p><code>system</code> also provides direct control over whether shell wildcard processing may take place, whereas backticks do not.</p>

<h2 id="How-can-I-call-backticks-without-shell-processing?"><a class="permalink" href="perlfaq8.html#How-can-I-call-backticks-without-shell-processing?">#</a><a id="How21"></a><a id="How-can-I-call-backticks-without-shell-processing"></a>How can I call backticks without shell processing?</h2>

<p>This is a bit tricky. You can&#39;t simply write the command like this:</p>

<pre><code>@ok = `grep @opts &#39;$search_string&#39; @filenames`;</code></pre>

<p>As of Perl 5.8.0, you can use <code>open()</code> with multiple arguments. Just like the list forms of <code>system()</code> and <code>exec()</code>, no shell escapes happen.</p>

<pre><code>open( GREP, &quot;-|&quot;, &#39;grep&#39;, @opts, $search_string, @filenames );
chomp(@ok = &lt;GREP&gt;);
close GREP;</code></pre>

<p>You can also:</p>

<pre><code>my @ok = ();
if (open(GREP, &quot;-|&quot;)) {
    while (&lt;GREP&gt;) {
        chomp;
        push(@ok, $_);
    }
    close GREP;
} else {
    exec &#39;grep&#39;, @opts, $search_string, @filenames;
}</code></pre>

<p>Just as with <code>system()</code>, no shell escapes happen when you <code>exec()</code> a list. Further examples of this can be found in <a href="perlipc.html#Safe-Pipe-Opens">&quot;Safe Pipe Opens&quot; in perlipc</a>.</p>

<p>Note that if you&#39;re using Windows, no solution to this vexing issue is even possible. Even though Perl emulates <code>fork()</code>, you&#39;ll still be stuck, because Windows does not have an argc/argv-style API.</p>

<h2 id="Why-can&#39;t-my-script-read-from-STDIN-after-I-gave-it-EOF-(^D-on-Unix,-^Z-on-MS-DOS)?"><a class="permalink" href="perlfaq8.html#Why-can&#39;t-my-script-read-from-STDIN-after-I-gave-it-EOF-(%5ED-on-Unix,-%5EZ-on-MS-DOS)?">#</a><a id="Why4"></a><a id="Why-cant-my-script-read-from-STDIN-after-I-gave-it-EOF-D-on-Unix-Z-on-MS-DOS"></a>Why can&#39;t my script read from STDIN after I gave it EOF (^D on Unix, ^Z on MS-DOS)?</h2>

<p>This happens only if your perl is compiled to use stdio instead of perlio, which is the default. Some (maybe all?) stdios set error and eof flags that you may need to clear. The <a href="POSIX.html">POSIX</a> module defines <code>clearerr()</code> that you can use. That is the technically correct way to do it. Here are some less reliable workarounds:</p>

<ol>

<li><p>Try keeping around the seekpointer and go there, like this:</p>

<pre><code>my $where = tell($log_fh);
seek($log_fh, $where, 0);</code></pre>

</li>
<li><p>If that doesn&#39;t work, try seeking to a different part of the file and then back.</p>

</li>
<li><p>If that doesn&#39;t work, try seeking to a different part of the file, reading something, and then seeking back.</p>

</li>
<li><p>If that doesn&#39;t work, give up on your stdio package and use sysread.</p>

</li>
</ol>

<h2 id="How-can-I-convert-my-shell-script-to-perl?"><a class="permalink" href="perlfaq8.html#How-can-I-convert-my-shell-script-to-perl?">#</a><a id="How22"></a><a id="How-can-I-convert-my-shell-script-to-perl"></a>How can I convert my shell script to perl?</h2>

<p>Learn Perl and rewrite it. Seriously, there&#39;s no simple converter. Things that are awkward to do in the shell are easy to do in Perl, and this very awkwardness is what would make a shell-&gt;perl converter nigh-on impossible to write. By rewriting it, you&#39;ll think about what you&#39;re really trying to do, and hopefully will escape the shell&#39;s pipeline datastream paradigm, which while convenient for some matters, causes many inefficiencies.</p>

<h2 id="Can-I-use-perl-to-run-a-telnet-or-ftp-session?"><a class="permalink" href="perlfaq8.html#Can-I-use-perl-to-run-a-telnet-or-ftp-session?">#</a><a id="Can"></a><a id="Can-I-use-perl-to-run-a-telnet-or-ftp-session"></a>Can I use perl to run a telnet or ftp session?</h2>

<p>Try the <a href="./Net::FTP.html">Net::FTP</a>, <a href="https://perldoc.perl.org/5.35.0/TCP::Client">TCP::Client</a>, and <a href="./Net::Telnet.html">Net::Telnet</a> modules (available from CPAN). <a href="http://www.cpan.org/scripts/netstuff/telnet.emul.shar">http://www.cpan.org/scripts/netstuff/telnet.emul.shar</a> will also help for emulating the telnet protocol, but <a href="./Net::Telnet.html">Net::Telnet</a> is quite probably easier to use.</p>

<p>If all you want to do is pretend to be telnet but don&#39;t need the initial telnet handshaking, then the standard dual-process approach will suffice:</p>

<pre><code>use IO::Socket;             # new in 5.004
my $handle = IO::Socket::INET-&gt;new(&#39;www.perl.com:80&#39;)
    or die &quot;can&#39;t connect to port 80 on www.perl.com $!&quot;;
$handle-&gt;autoflush(1);
if (fork()) {               # XXX: undef means failure
    select($handle);
    print while &lt;STDIN&gt;;    # everything from stdin to socket
} else {
    print while &lt;$handle&gt;;  # everything from socket to stdout
}
close $handle;
exit;</code></pre>

<h2 id="How-can-I-write-expect-in-Perl?"><a class="permalink" href="perlfaq8.html#How-can-I-write-expect-in-Perl?">#</a><a id="How23"></a><a id="How-can-I-write-expect-in-Perl"></a>How can I write expect in Perl?</h2>

<p>Once upon a time, there was a library called <i>chat2.pl</i> (part of the standard perl distribution), which never really got finished. If you find it somewhere, <i>don&#39;t use it</i>. These days, your best bet is to look at the <a href="Expect.html">Expect</a> module available from CPAN, which also requires two other modules from CPAN, <a href="./IO::Pty.html">IO::Pty</a> and <a href="./IO::Stty.html">IO::Stty</a>.</p>

<h2 id="Is-there-a-way-to-hide-perl&#39;s-command-line-from-programs-such-as-&quot;ps&quot;?"><a class="permalink" href="perlfaq8.html#Is-there-a-way-to-hide-perl&#39;s-command-line-from-programs-such-as-%22ps%22?">#</a><a id="Is"></a><a id="Is-there-a-way-to-hide-perls-command-line-from-programs-such-as-ps"></a>Is there a way to hide perl&#39;s command line from programs such as &quot;ps&quot;?</h2>

<p>First of all note that if you&#39;re doing this for security reasons (to avoid people seeing passwords, for example) then you should rewrite your program so that critical information is never given as an argument. Hiding the arguments won&#39;t make your program completely secure.</p>

<p>To actually alter the visible command line, you can assign to the variable $0 as documented in <a href="perlvar.html">perlvar</a>. This won&#39;t work on all operating systems, though. Daemon programs like sendmail place their state there, as in:</p>

<pre><code>$0 = &quot;orcus [accepting connections]&quot;;</code></pre>

<h2 id="I-{changed-directory,-modified-my-environment}-in-a-perl-script.-How-come-the-change-disappeared-when-I-exited-the-script?-How-do-I-get-my-changes-to-be-visible?"><a class="permalink" href="perlfaq8.html#I-%7Bchanged-directory,-modified-my-environment%7D-in-a-perl-script.-How-come-the-change-disappeared-when-I-exited-the-script?-How-do-I-get-my-changes-to-be-visible?">#</a><a id="I"></a><a id="I-changed-directory-modified-my-environment-in-a-perl-script.-How-come-the-change-disappeared-when-I-exited-the-script-How-do-I-get-my-changes-to-be-visible"></a>I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible?</h2>

<dl>

<dt id="Unix"><a class="permalink" href="perlfaq8.html#Unix">#</a>Unix</dt>
<dd>

<p>In the strictest sense, it can&#39;t be done--the script executes as a different process from the shell it was started from. Changes to a process are not reflected in its parent--only in any children created after the change. There is shell magic that may allow you to fake it by <code>eval()</code>ing the script&#39;s output in your shell; check out the comp.unix.questions FAQ for details.</p>

</dd>
</dl>

<h2 id="How-do-I-close-a-process&#39;s-filehandle-without-waiting-for-it-to-complete?"><a class="permalink" href="perlfaq8.html#How-do-I-close-a-process&#39;s-filehandle-without-waiting-for-it-to-complete?">#</a><a id="How24"></a><a id="How-do-I-close-a-processs-filehandle-without-waiting-for-it-to-complete"></a>How do I close a process&#39;s filehandle without waiting for it to complete?</h2>

<p>Assuming your system supports such things, just send an appropriate signal to the process (see <a href="perlfunc.html#kill">&quot;kill&quot; in perlfunc</a>). It&#39;s common to first send a TERM signal, wait a little bit, and then send a KILL signal to finish it off.</p>

<h2 id="How-do-I-fork-a-daemon-process?"><a class="permalink" href="perlfaq8.html#How-do-I-fork-a-daemon-process?">#</a><a id="How25"></a><a id="How-do-I-fork-a-daemon-process"></a>How do I fork a daemon process?</h2>

<p>If by daemon process you mean one that&#39;s detached (disassociated from its tty), then the following process is reported to work on most Unixish systems. Non-Unix users should check their Your_OS::Process module for other solutions.</p>

<ul>

<li><p>Open /dev/tty and use the TIOCNOTTY ioctl on it. See <a href="http://man.he.net/man1/tty">tty(1)</a> for details. Or better yet, you can just use the <code>POSIX::setsid()</code> function, so you don&#39;t have to worry about process groups.</p>

</li>
<li><p>Change directory to /</p>

</li>
<li><p>Reopen STDIN, STDOUT, and STDERR so they&#39;re not connected to the old tty.</p>

</li>
<li><p>Background yourself like this:</p>

<pre><code>fork &amp;&amp; exit;</code></pre>

</li>
</ul>

<p>The <a href="./Proc::Daemon.html">Proc::Daemon</a> module, available from CPAN, provides a function to perform these actions for you.</p>

<h2 id="How-do-I-find-out-if-I&#39;m-running-interactively-or-not?"><a class="permalink" href="perlfaq8.html#How-do-I-find-out-if-I&#39;m-running-interactively-or-not?">#</a><a id="How26"></a><a id="How-do-I-find-out-if-Im-running-interactively-or-not"></a>How do I find out if I&#39;m running interactively or not?</h2>

<p>(contributed by brian d foy)</p>

<p>This is a difficult question to answer, and the best answer is only a guess.</p>

<p>What do you really want to know? If you merely want to know if one of your filehandles is connected to a terminal, you can try the <code>-t</code> file test:</p>

<pre><code>if( -t STDOUT ) {
    print &quot;I&#39;m connected to a terminal!\n&quot;;
}</code></pre>

<p>However, you might be out of luck if you expect that means there is a real person on the other side. With the <a href="Expect.html">Expect</a> module, another program can pretend to be a person. The program might even come close to passing the Turing test.</p>

<p>The <a href="./IO::Interactive.html">IO::Interactive</a> module does the best it can to give you an answer. Its <code>is_interactive</code> function returns an output filehandle; that filehandle points to standard output if the module thinks the session is interactive. Otherwise, the filehandle is a null handle that simply discards the output:</p>

<pre><code>use IO::Interactive;

print { is_interactive } &quot;I might go to standard output!\n&quot;;</code></pre>

<p>This still doesn&#39;t guarantee that a real person is answering your prompts or reading your output.</p>

<p>If you want to know how to handle automated testing for your distribution, you can check the environment. The CPAN Testers, for instance, set the value of <code>AUTOMATED_TESTING</code>:</p>

<pre><code>unless( $ENV{AUTOMATED_TESTING} ) {
    print &quot;Hello interactive tester!\n&quot;;
}</code></pre>

<h2 id="How-do-I-timeout-a-slow-event?"><a class="permalink" href="perlfaq8.html#How-do-I-timeout-a-slow-event?">#</a><a id="How27"></a><a id="How-do-I-timeout-a-slow-event"></a>How do I timeout a slow event?</h2>

<p>Use the <code>alarm()</code> function, probably in conjunction with a signal handler, as documented in <a href="perlipc.html#Signals">&quot;Signals&quot; in perlipc</a> and the section on &quot;Signals&quot; in the Camel. You may instead use the more flexible <a href="./Sys::AlarmCall.html">Sys::AlarmCall</a> module available from CPAN.</p>

<p>The <code>alarm()</code> function is not implemented on all versions of Windows. Check the documentation for your specific version of Perl.</p>

<h2 id="How-do-I-set-CPU-limits?"><a class="permalink" href="perlfaq8.html#How-do-I-set-CPU-limits?">#</a><a id="How28"></a><a id="How-do-I-set-CPU-limits"></a>How do I set CPU limits?   </h2>

<p>(contributed by Xho)</p>

<p>Use the <a href="./BSD::Resource.html">BSD::Resource</a> module from CPAN. As an example:</p>

<pre><code>use BSD::Resource;
setrlimit(RLIMIT_CPU,10,20) or die $!;</code></pre>

<p>This sets the soft and hard limits to 10 and 20 seconds, respectively. After 10 seconds of time spent running on the CPU (not &quot;wall&quot; time), the process will be sent a signal (XCPU on some systems) which, if not trapped, will cause the process to terminate. If that signal is trapped, then after 10 more seconds (20 seconds in total) the process will be killed with a non-trappable signal.</p>

<p>See the <a href="./BSD::Resource.html">BSD::Resource</a> and your systems documentation for the gory details.</p>

<h2 id="How-do-I-avoid-zombies-on-a-Unix-system?"><a class="permalink" href="perlfaq8.html#How-do-I-avoid-zombies-on-a-Unix-system?">#</a><a id="How29"></a><a id="How-do-I-avoid-zombies-on-a-Unix-system"></a>How do I avoid zombies on a Unix system?</h2>

<p>Use the reaper code from <a href="perlipc.html#Signals">&quot;Signals&quot; in perlipc</a> to call <code>wait()</code> when a SIGCHLD is received, or else use the double-fork technique described in <a href="perlfaq8.html#How-do-I-start-a-process-in-the-background%3F">&quot;How do I start a process in the background?&quot; in perlfaq8</a>.</p>

<h2 id="How-do-I-use-an-SQL-database?"><a class="permalink" href="perlfaq8.html#How-do-I-use-an-SQL-database?">#</a><a id="How30"></a><a id="How-do-I-use-an-SQL-database"></a>How do I use an SQL database?</h2>

<p>The <a href="DBI.html">DBI</a> module provides an abstract interface to most database servers and types, including Oracle, DB2, Sybase, mysql, Postgresql, ODBC, and flat files. The DBI module accesses each database type through a database driver, or DBD. You can see a complete list of available drivers on CPAN: <a href="http://www.cpan.org/modules/by-module/DBD/">http://www.cpan.org/modules/by-module/DBD/</a> . You can read more about DBI on <a href="http://dbi.perl.org/">http://dbi.perl.org/</a> .</p>

<p>Other modules provide more specific access: <a href="./Win32::ODBC.html">Win32::ODBC</a>, <a href="Alzabo.html">Alzabo</a>, <code>iodbc</code>, and others found on CPAN Search: <a href="https://metacpan.org/">https://metacpan.org/</a> .</p>

<h2 id="How-do-I-make-a-system()-exit-on-control-C?"><a class="permalink" href="perlfaq8.html#How-do-I-make-a-system()-exit-on-control-C?">#</a><a id="How31"></a><a id="How-do-I-make-a-system-exit-on-control-C"></a>How do I make a system() exit on control-C?</h2>

<p>You can&#39;t. You need to imitate the <code>system()</code> call (see <a href="perlipc.html">perlipc</a> for sample code) and then have a signal handler for the INT signal that passes the signal on to the subprocess. Or you can check for it:</p>

<pre><code>$rc = system($cmd);
if ($rc &amp; 127) { die &quot;signal death&quot; }</code></pre>

<h2 id="How-do-I-open-a-file-without-blocking?"><a class="permalink" href="perlfaq8.html#How-do-I-open-a-file-without-blocking?">#</a><a id="How32"></a><a id="How-do-I-open-a-file-without-blocking"></a>How do I open a file without blocking?</h2>

<p>If you&#39;re lucky enough to be using a system that supports non-blocking reads (most Unixish systems do), you need only to use the <code>O_NDELAY</code> or <code>O_NONBLOCK</code> flag from the <code>Fcntl</code> module in conjunction with <code>sysopen()</code>:</p>

<pre><code>use Fcntl;
sysopen(my $fh, &quot;/foo/somefile&quot;, O_WRONLY|O_NDELAY|O_CREAT, 0644)
    or die &quot;can&#39;t open /foo/somefile: $!&quot;:</code></pre>

<h2 id="How-do-I-tell-the-difference-between-errors-from-the-shell-and-perl?"><a class="permalink" href="perlfaq8.html#How-do-I-tell-the-difference-between-errors-from-the-shell-and-perl?">#</a><a id="How33"></a><a id="How-do-I-tell-the-difference-between-errors-from-the-shell-and-perl"></a>How do I tell the difference between errors from the shell and perl?</h2>

<p>(answer contributed by brian d foy)</p>

<p>When you run a Perl script, something else is running the script for you, and that something else may output error messages. The script might emit its own warnings and error messages. Most of the time you cannot tell who said what.</p>

<p>You probably cannot fix the thing that runs perl, but you can change how perl outputs its warnings by defining a custom warning and die functions.</p>

<p>Consider this script, which has an error you may not notice immediately.</p>

<pre><code>#!/usr/locl/bin/perl

print &quot;Hello World\n&quot;;</code></pre>

<p>I get an error when I run this from my shell (which happens to be bash). That may look like perl forgot it has a <code>print()</code> function, but my shebang line is not the path to perl, so the shell runs the script, and I get the error.</p>

<pre><code class="plaintext">$ ./test
./test: line 3: print: command not found</code></pre>

<p>A quick and dirty fix involves a little bit of code, but this may be all you need to figure out the problem.</p>

<pre><code>#!/usr/bin/perl -w

BEGIN {
    $SIG{__WARN__} = sub{ print STDERR &quot;Perl: &quot;, @_; };
    $SIG{__DIE__}  = sub{ print STDERR &quot;Perl: &quot;, @_; exit 1};
}

$a = 1 + undef;
$x / 0;
__END__</code></pre>

<p>The perl message comes out with &quot;Perl&quot; in front. The <code>BEGIN</code> block works at compile time so all of the compilation errors and warnings get the &quot;Perl:&quot; prefix too.</p>

<pre><code class="plaintext">Perl: Useless use of division (/) in void context at ./test line 9.
Perl: Name &quot;main::a&quot; used only once: possible typo at ./test line 8.
Perl: Name &quot;main::x&quot; used only once: possible typo at ./test line 9.
Perl: Use of uninitialized value in addition (+) at ./test line 8.
Perl: Use of uninitialized value in division (/) at ./test line 9.
Perl: Illegal division by zero at ./test line 9.
Perl: Illegal division by zero at -e line 3.</code></pre>

<p>If I don&#39;t see that &quot;Perl:&quot;, it&#39;s not from perl.</p>

<p>You could also just know all the perl errors, and although there are some people who may know all of them, you probably don&#39;t. However, they all should be in the <a href="perldiag.html">perldiag</a> manpage. If you don&#39;t find the error in there, it probably isn&#39;t a perl error.</p>

<p>Looking up every message is not the easiest way, so let perl to do it for you. Use the diagnostics pragma with turns perl&#39;s normal messages into longer discussions on the topic.</p>

<pre><code>use diagnostics;</code></pre>

<p>If you don&#39;t get a paragraph or two of expanded discussion, it might not be perl&#39;s message.</p>

<h2 id="How-do-I-install-a-module-from-CPAN?"><a class="permalink" href="perlfaq8.html#How-do-I-install-a-module-from-CPAN?">#</a><a id="How34"></a><a id="How-do-I-install-a-module-from-CPAN"></a>How do I install a module from CPAN?</h2>

<p>(contributed by brian d foy)</p>

<p>The easiest way is to have a module also named CPAN do it for you by using the <code>cpan</code> command that comes with Perl. You can give it a list of modules to install:</p>

<pre><code class="plaintext">$ cpan IO::Interactive Getopt::Whatever</code></pre>

<p>If you prefer <code>CPANPLUS</code>, it&#39;s just as easy:</p>

<pre><code class="plaintext">$ cpanp i IO::Interactive Getopt::Whatever</code></pre>

<p>If you want to install a distribution from the current directory, you can tell <code>CPAN.pm</code> to install <code>.</code> (the full stop):</p>

<pre><code class="plaintext">$ cpan .</code></pre>

<p>See the documentation for either of those commands to see what else you can do.</p>

<p>If you want to try to install a distribution by yourself, resolving all dependencies on your own, you follow one of two possible build paths.</p>

<p>For distributions that use <i>Makefile.PL</i>:</p>

<pre><code class="plaintext">$ perl Makefile.PL
$ make test install</code></pre>

<p>For distributions that use <i>Build.PL</i>:</p>

<pre><code class="plaintext">$ perl Build.PL
$ ./Build test
$ ./Build install</code></pre>

<p>Some distributions may need to link to libraries or other third-party code and their build and installation sequences may be more complicated. Check any <i>README</i> or <i>INSTALL</i> files that you may find.</p>

<h2 id="What&#39;s-the-difference-between-require-and-use?"><a class="permalink" href="perlfaq8.html#What&#39;s-the-difference-between-require-and-use?">#</a><a id="What1"></a><a id="Whats-the-difference-between-require-and-use"></a>What&#39;s the difference between require and use?</h2>

<p>(contributed by brian d foy)</p>

<p>Perl runs <code>require</code> statement at run-time. Once Perl loads, compiles, and runs the file, it doesn&#39;t do anything else. The <code>use</code> statement is the same as a <code>require</code> run at compile-time, but Perl also calls the <code>import</code> method for the loaded package. These two are the same:</p>

<pre><code>use MODULE qw(import list);

BEGIN {
    require MODULE;
    MODULE-&gt;import(import list);
}</code></pre>

<p>However, you can suppress the <code>import</code> by using an explicit, empty import list. Both of these still happen at compile-time:</p>

<pre><code>use MODULE ();

BEGIN {
    require MODULE;
}</code></pre>

<p>Since <code>use</code> will also call the <code>import</code> method, the actual value for <code>MODULE</code> must be a bareword. That is, <code>use</code> cannot load files by name, although <code>require</code> can:</p>

<pre><code>require &quot;$ENV{HOME}/lib/Foo.pm&quot;; # no @INC searching!</code></pre>

<p>See the entry for <code>use</code> in <a href="perlfunc.html">perlfunc</a> for more details.</p>

<h2 id="How-do-I-keep-my-own-module/library-directory?"><a class="permalink" href="perlfaq8.html#How-do-I-keep-my-own-module/library-directory?">#</a><a id="How35"></a><a id="How-do-I-keep-my-own-module-library-directory"></a>How do I keep my own module/library directory?</h2>

<p>When you build modules, tell Perl where to install the modules.</p>

<p>If you want to install modules for your own use, the easiest way might be <a href="./local::lib.html">local::lib</a>, which you can download from CPAN. It sets various installation settings for you, and uses those same settings within your programs.</p>

<p>If you want more flexibility, you need to configure your CPAN client for your particular situation.</p>

<p>For <code>Makefile.PL</code>-based distributions, use the INSTALL_BASE option when generating Makefiles:</p>

<pre><code class="plaintext">perl Makefile.PL INSTALL_BASE=/mydir/perl</code></pre>

<p>You can set this in your <code>CPAN.pm</code> configuration so modules automatically install in your private library directory when you use the CPAN.pm shell:</p>

<pre><code class="plaintext">% cpan
cpan&gt; o conf makepl_arg INSTALL_BASE=/mydir/perl
cpan&gt; o conf commit</code></pre>

<p>For <code>Build.PL</code>-based distributions, use the --install_base option:</p>

<pre><code class="plaintext">perl Build.PL --install_base /mydir/perl</code></pre>

<p>You can configure <code>CPAN.pm</code> to automatically use this option too:</p>

<pre><code class="plaintext">% cpan
cpan&gt; o conf mbuild_arg &quot;--install_base /mydir/perl&quot;
cpan&gt; o conf commit</code></pre>

<p>INSTALL_BASE tells these tools to put your modules into <i>/mydir/perl/lib/perl5</i>. See <a href="perlfaq8.html#How-do-I-add-a-directory-to-my-include-path-%28%40INC%29-at-runtime%3F">&quot;How do I add a directory to my include path (@INC) at runtime?&quot;</a> for details on how to run your newly installed modules.</p>

<p>There is one caveat with INSTALL_BASE, though, since it acts differently from the PREFIX and LIB settings that older versions of <a href="./ExtUtils::MakeMaker.html">ExtUtils::MakeMaker</a> advocated. INSTALL_BASE does not support installing modules for multiple versions of Perl or different architectures under the same directory. You should consider whether you really want that and, if you do, use the older PREFIX and LIB settings. See the <a href="https://perldoc.perl.org/5.35.0/ExtUtils::Makemaker">ExtUtils::Makemaker</a> documentation for more details.</p>

<h2 id="How-do-I-add-the-directory-my-program-lives-in-to-the-module/library-search-path?"><a class="permalink" href="perlfaq8.html#How-do-I-add-the-directory-my-program-lives-in-to-the-module/library-search-path?">#</a><a id="How36"></a><a id="How-do-I-add-the-directory-my-program-lives-in-to-the-module-library-search-path"></a>How do I add the directory my program lives in to the module/library search path?</h2>

<p>(contributed by brian d foy)</p>

<p>If you know the directory already, you can add it to <code>@INC</code> as you would for any other directory. You might <code>use lib</code> if you know the directory at compile time:</p>

<pre><code>use lib $directory;</code></pre>

<p>The trick in this task is to find the directory. Before your script does anything else (such as a <code>chdir</code>), you can get the current working directory with the <code>Cwd</code> module, which comes with Perl:</p>

<pre><code>BEGIN {
    use Cwd;
    our $directory = cwd;
}

use lib $directory;</code></pre>

<p>You can do a similar thing with the value of <code>$0</code>, which holds the script name. That might hold a relative path, but <code>rel2abs</code> can turn it into an absolute path. Once you have the</p>

<pre><code>BEGIN {
    use File::Spec::Functions qw(rel2abs);
    use File::Basename qw(dirname);

    my $path   = rel2abs( $0 );
    our $directory = dirname( $path );
}

use lib $directory;</code></pre>

<p>The <a href="FindBin.html">FindBin</a> module, which comes with Perl, might work. It finds the directory of the currently running script and puts it in <code>$Bin</code>, which you can then use to construct the right library path:</p>

<pre><code>use FindBin qw($Bin);</code></pre>

<p>You can also use <a href="./local::lib.html">local::lib</a> to do much of the same thing. Install modules using <a href="./local::lib.html">local::lib</a>&#39;s settings then use the module in your program:</p>

<pre><code>use local::lib; # sets up a local lib at ~/perl5</code></pre>

<p>See the <a href="./local::lib.html">local::lib</a> documentation for more details.</p>

<h2 id="How-do-I-add-a-directory-to-my-include-path-(@INC)-at-runtime?"><a class="permalink" href="perlfaq8.html#How-do-I-add-a-directory-to-my-include-path-(@INC)-at-runtime?">#</a><a id="How37"></a><a id="How-do-I-add-a-directory-to-my-include-path-INC-at-runtime"></a>How do I add a directory to my include path (@INC) at runtime?</h2>

<p>Here are the suggested ways of modifying your include path, including environment variables, run-time switches, and in-code statements:</p>

<dl>

<dt id="the-PERLLIB-environment-variable"><a class="permalink" href="perlfaq8.html#the-PERLLIB-environment-variable">#</a><a id="the"></a>the <code>PERLLIB</code> environment variable</dt>
<dd>

<pre><code class="plaintext">$ export PERLLIB=/path/to/my/dir
$ perl program.pl</code></pre>

</dd>
<dt id="the-PERL5LIB-environment-variable"><a class="permalink" href="perlfaq8.html#the-PERL5LIB-environment-variable">#</a><a id="the1"></a>the <code>PERL5LIB</code> environment variable</dt>
<dd>

<pre><code class="plaintext">$ export PERL5LIB=/path/to/my/dir
$ perl program.pl</code></pre>

</dd>
<dt id="the-perl-Idir-command-line-flag"><a class="permalink" href="perlfaq8.html#the-perl-Idir-command-line-flag">#</a><a id="the2"></a><a id="the-perl--Idir-command-line-flag"></a>the <code>perl -Idir</code> command line flag</dt>
<dd>

<pre><code class="plaintext">$ perl -I/path/to/my/dir program.pl</code></pre>

</dd>
<dt id="the-lib-pragma:"><a class="permalink" href="perlfaq8.html#the-lib-pragma:">#</a><a id="the3"></a><a id="the-lib-pragma"></a>the <code>lib</code> pragma:</dt>
<dd>

<pre><code>use lib &quot;$ENV{HOME}/myown_perllib&quot;;</code></pre>

</dd>
<dt id="the-local::lib-module:"><a class="permalink" href="perlfaq8.html#the-local::lib-module:">#</a><a id="the4"></a><a id="the-local::lib-module"></a>the <a href="./local::lib.html">local::lib</a> module:</dt>
<dd>

<pre><code>use local::lib;

use local::lib &quot;~/myown_perllib&quot;;</code></pre>

</dd>
</dl>

<h2 id="Where-are-modules-installed?"><a class="permalink" href="perlfaq8.html#Where-are-modules-installed?">#</a><a id="Where1"></a><a id="Where-are-modules-installed"></a>Where are modules installed?</h2>

<p>Modules are installed on a case-by-case basis (as provided by the methods described in the previous section), and in the operating system. All of these paths are stored in @INC, which you can display with the one-liner</p>

<pre><code>perl -e &#39;print join(&quot;\n&quot;,@INC,&quot;&quot;)&#39;</code></pre>

<p>The same information is displayed at the end of the output from the command</p>

<pre><code class="plaintext">perl -V</code></pre>

<p>To find out where a module&#39;s source code is located, use</p>

<pre><code class="plaintext">perldoc -l Encode</code></pre>

<p>to display the path to the module. In some cases (for example, the <code>AutoLoader</code> module), this command will show the path to a separate <code>pod</code> file; the module itself should be in the same directory, with a &#39;pm&#39; file extension.</p>

<h2 id="What-is-socket.ph-and-where-do-I-get-it?"><a class="permalink" href="perlfaq8.html#What-is-socket.ph-and-where-do-I-get-it?">#</a><a id="What2"></a><a id="What-is-socket.ph-and-where-do-I-get-it"></a>What is socket.ph and where do I get it?</h2>

<p>It&#39;s a Perl 4 style file defining values for system networking constants. Sometimes it is built using <a href="h2ph.html">h2ph</a> when Perl is installed, but other times it is not. Modern programs should use <code>use Socket;</code> instead.</p>

<h1 id="AUTHOR-AND-COPYRIGHT"><a class="permalink" href="perlfaq8.html#AUTHOR-AND-COPYRIGHT">#</a><a id="AUTHOR"></a>AUTHOR AND COPYRIGHT</h1>

<p>Copyright (c) 1997-2010 Tom Christiansen, Nathan Torkington, and other authors as noted. All rights reserved.</p>

<p>This documentation is free; you can redistribute it and/or modify it under the same terms as Perl itself.</p>

<p>Irrespective of its distribution, all code examples in this file are hereby placed into the public domain. You are permitted and encouraged to use this code in your own programs for fun or for profit as you see fit. A simple comment in the code giving credit would be courteous but is not required.</p>


      </div>
      <div id="footer">
        <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p>

<p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p>


      </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    <script src="../highlight.pack.js"></script>
    <script>hljs.highlightAll();</script>
  </body>
</html>
